var tempUserList = []; //临时的用户列表
var page = 1; //用户列表的页数
var searchType = "全部"; //选择的分类
var isAdmination = 0; //是否在倒计时
var time = 0; //倒计时
var t = null; //定时器
var service_view = require("../../service/view.js");
var service_common = require("../../service/common.js");
var service_user = require("../../service/user.js");
var service_pay = require("../../service/pay.js");
var service_version = require("../../service/version.js");
var service_adv = require("../../service/adv.js");
Page({
	/**
	 * 页面的初始数据
	 */
	data: {
		userList: [],
		userInfo: [],
		nowTime: Date.parse(new Date()) / 1000,
		typeArray: getApp().globalData.typeArray,
		time_css: "display:none;",
		day: "",
		time: "",
		wrq_news: 0,
		user_news: 0,
		offon: false
	},
	/**
	 * 获取展示时间
	 */
	showTime: function() {
		var that = this;
		service_user.getShowTime(function(res) {
			if (res.data.data > 0) {
				clearTimeout(t);
				time = 0;
				time = res.data.data;
				timeOut(that, time);
			} else {
				clearTimeout(t);
				time = 0;
				that.setData({
					day: "",
					time: "",
					time_css: "display:none;",
				})
			}
		})
	},
	/**
	 * 页面加载
	 */
	onLoad: function() {
		var that = this;
		var itemColor = [];
		var itemSize = [];
		var typeArray = [];
		itemColor[0] = "#528aee";
		itemSize[0] = "19px";
		typeArray[0] = [0, "#528aee", "19px", getApp().globalData.typeArray[0], "border-bottom:2px solid #528aee;"];
		for (var i = 1; i < getApp().globalData.typeArray.length; i++) {
			itemColor[i] = "black";
			itemSize[i] = "18px";
			typeArray[i] = [i, "black", "18px", getApp().globalData.typeArray[i], ""];
		}
		that.setData({
			typeArray: typeArray,
			itemColor: itemColor,
			itemSize: itemSize
		})
		this.showTime()
		setInterval(function() {
			that.setData({
				scrollHide: "opacity:1;"
			})
		}, 1000)
		this.getUserList();
	},
	/**
	 * 页面渲染
	 */
	onShow: function() {
		var that = this;
		service_view.newReplyCount(this.newReplyCount);
		page = 1;
		this.getUserList();
		service_version.getIsAuditing(function(res) {
			if (res.data.status == 0) {
				that.setData({
					isCheck: false,
					no_data: "无数据"
				})
			} else {
				that.setData({
					isCheck: true
				})
			}
		})
		// this.User_new()
		// this.wrq_zan()
	},
	// 获取万人圈点赞个数
	wrq_zan: function() {
		service_common.fetchData(
			service_common.getUrl("user.newLoveCount"), {
				openid: getApp().globalData.openid
			},
			(res) => {
				// 
				this.setData({
					wrq_zan: res.data.data.new_love_count
				})
			},
			"GET"
		)
	},
	// 获取后台消息
	User_new: function() {
		service_adv.getAdvList((res) => {
			console.log('------++++++')
			var n = res.data.data.data.length
			service_adv.getSeeAdv(res => {
				this.setData({
					user_news: n - res.data.data.see_adv_list.length
				})
			})
		})
	},
	toAdv: function() {
		wx.navigateTo({
			url: '/pages/adv_list/adv_list',
		})
	},
	/**
	 * 获取万人圈新消息数
	 */
	newReplyCount: function(res) {
		this.setData({
			wrq_news: res.data.data.new_reply_count
		})
	},
	/**
	 * 下拉刷新
	 */
	onPullDownRefresh: function() {
		var that = this;
		tempUserList = [];
		page = 1;
		this.getUserList();
		this.showTime();
	},
	/**
	 * 获取用户列表
	 */
	getUserList: function() {
		var that = this;
		service_view.getFindList(page, searchType, function(res) {
			var data = res.data.data.data;
			if (data.length > 0) {
				for (var i = 0; i < data.length; i++) {
					var exsit = false;
					for (var j = 0; j < tempUserList.length; j++) {
						if (tempUserList[j].openid == data[i].openid) {
							exsit = true;
							break;
						}
					}
					if (!exsit) {
						tempUserList.push(data[i]);
					}
				}
				that.getUserListCB();
			} else {
				if (that.data.userList.length > 0) {
					wx.showToast({
						title: '已到底啦',
						image: '../../image/end.png',
						duration: 500
					});
				}
				wx.stopPullDownRefresh();
			}
			page++;
		})
	},
	/**
	 * 获取用户列表回调函数
	 */
	getUserListCB: function() {
		this.setData({
			userList: tempUserList
		});
		wx.hideLoading();
		wx.stopPullDownRefresh();
	},
	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {
		wx.showToast({
			title: '加载数据中',
			icon: 'loading',
			duration: 100000
		});
		this.getUserList();
	},
	/**
	 * 点击标签事件
	 */
	itemSelect: function(e) {
		var typeArray = [];
		searchType = e.target.dataset.type;
		page = 1;
		for (var i = 0; i <= getApp().globalData.typeArray.length - 1; i++) {
			if (e.target.dataset.item == i) {
				typeArray[i] = [i, "#528aee", "19px", getApp().globalData.typeArray[i], "border-bottom:2px solid #528aee;"];
			} else {
				typeArray[i] = [i, "black", "18px", getApp().globalData.typeArray[i], ""];
			}
		}
		tempUserList = [];
		this.setData({
			userList: [],
			typeArray: typeArray
		});

		this.getUserList();
	},
	/**
	 * 跳转到Find页面
	 */
	toFind: function(e) {
		console.log(e);
		wx.navigateTo({
			url: "/pages/find/find?openid=" + e.currentTarget.dataset.openid
		})
	},
	/**
	 * 跳转到首页
	 */
	to_share_submit: function(e) {
		add_form_id(e.detail.formId)
		var pages = getCurrentPages()
		wx.switchTab({
      url: "/page/index/index",
		})
		getApp().globalData.currentStatu = "open"
	},
	/**
	 * 开通百万曝光
	 */
	pay: function(e) {
		wx.showModal({
			title: '提示',
			content: '需要前去下载APP',
			confirmText: "前往",
			success: function(res) {
				if (res.confirm) {
					wx.navigateTo({
						url: '/pages/aApp/aApp',
					})
				}
			}
		})
	},
	/**
	 * 页面滑动时间
	 */
	onPageScroll: function() {
		var that = this;
		that.setData({
			scrollHide: "opacity:0;"
		})
	},
	/**
	 * 我想出现在此
	 */
	powerDrawer: function(e) {
		var currentStatu = e.currentTarget.dataset.statu;
		this.util(currentStatu)
	},
	// 关闭分享
	close_: function() {
		this.setData({
			offon: true
		})
	},
    sendShare: function(){
        
    },
	//转发设置
	onShareAppMessage: function() {

		// 设置转发的路径
		var share_path = 'pages/other/other?openid=' + getApp().globalData.userInfo.openid + "&r=" + Math.random(100000);
		return {
			title: "我是" + getApp().globalData.userInfo.nickname + getApp().globalData.share_title,
			desc: '一个名片社交的资源配对平台，找资源找合作你就来',
			path: share_path,
			success: function() {
				service_user.setUserShareTime(function(res) {
					if (res.data.code == 0) {
						wx.showToast({
							title: '递名片成功',
							icon: 'success',
							duration: 1000,
						});
						setTimeout(function() {
							wx.showToast({
								title: '获得一天展示权限！',
								icon: 'success',
								duration: 1000,
							});
						}, 1000)
					} else {
						wx.showToast({
							title: '递名片成功',
							icon: 'success',
							duration: 1500
						});
					}
				})
			},
			fail: res => {
				wx.showToast({
					title: '递名片失败',
					image: '../../image/cancel.png',
					duration: 2000
				});
			}
		}
	},
	/**
	 * 动画
	 */
	util: function(currentStatu) {
		/* 动画部分 */
		// 第1步：创建动画实例   
		var animation = wx.createAnimation({
			duration: 200, //动画时长  
			timingFunction: "linear", //线性  
			delay: 0 //0则不延迟  
		});

		// 第2步：这个动画实例赋给当前的动画实例  
		this.animation = animation;

		// 第3步：执行第一组动画  
		animation.opacity(0).rotateX(-100).step();

		// 第4步：导出动画对象赋给数据对象储存  
		this.setData({
			animationData: animation.export()
		})

		// 第5步：设置定时器到指定时候后，执行第二组动画  
		setTimeout(function() {
			// 执行第二组动画  
			animation.opacity(1).rotateX(0).step();
			// 给数据对象储存的第一组动画，更替为执行完第二组动画的动画对象  
			this.setData({
				animationData: animation
			})

			//关闭  
			if (currentStatu == "close") {
				this.setData({
					showModalStatus: false
				});
			}
		}.bind(this), 200)

		// 显示  
		if (currentStatu == "open") {
			this.setData({
				showModalStatus: true
			});
		}
	}
})
/**
 * 添加form_id
 */
function add_form_id(form_id) {
	wx.request({
		url: getApp().globalData.API_Base_URL + '/formid/index/setFormId?openid=' + getApp().globalData.openid + '&formid=' +
			form_id,
	})
}
/**
 * 展示时间倒计时
 */
function timeOut(that, time) {
	t = setInterval(function() {
		time = time - 1;
		var day = Math.floor(time / 3600 / 24);
		var hr = Math.floor(time / 3600 % 24);
		if (hr < 10) {
			hr = "0" + hr;
		}
		var min = Math.floor(time / 60 % 60);
		if (min < 10) {
			min = "0" + min;
		}
		var sec = Math.floor(time % 60);
		if (sec < 10) {
			sec = "0" + sec;
		}
		that.setData({
			day: day + "天",
			time: hr + ":" + min + ":" + sec,
			time_css: "",
		})
	}, 1000);
	that.setData({
		time_css: "",
	})
}
